package mytaxy.tpagp.Map;

import java.util.ArrayList;

import mytaxy.tpagp.R;
import mytaxy.tpagp.R.drawable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MapOverlay extends Overlay {
    
    private GeoPoint point_act;
    private GeoPoint geoStop;
    private GeoPoint geoT1; 
    ArrayList<GeoPoint> mTaxys = new ArrayList<GeoPoint>();;
    
    
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();
        if (shadow == false) {
            Point centro = new Point();
            //System.out.println(point);
            projection.toPixels(point_act, centro);

            //Definimos el pincel de dibujo
            Paint p = new Paint();
            p.setColor(Color.BLUE);
            
            //Marca Ejemplo 2: Bitmap
            Bitmap bm = BitmapFactory.decodeResource(
                    mapView.getResources(), 
                    R.drawable.gmarker);
            
            canvas.drawBitmap(bm, centro.x - bm.getWidth()/2, centro.y - bm.getHeight(), p);
            
            // Parada de Taxi
            Point taxi_stop_1 = new Point();
            projection.toPixels(geoStop, taxi_stop_1);
            
            Bitmap stop1 = BitmapFactory.decodeResource(mapView.getResources(), R.drawable.taxi_stop);
            canvas.drawBitmap(stop1, taxi_stop_1.x - stop1.getWidth()/2,taxi_stop_1.y - stop1.getHeight()/2, p);
            mTaxys.add(geoStop);
            
            // Pintar Taxi1:
            // GeoPoint geoT1 = new GeoPoint(41389137, 2173491);
            Point point_taxi_1 = new Point();
            projection.toPixels(geoT1, point_taxi_1);
            
            Bitmap taxi1 = BitmapFactory.decodeResource(
                    mapView.getResources(), 
                    R.drawable.logo_taxi);
            canvas.drawBitmap(taxi1, point_taxi_1.x - taxi1.getWidth()/2,point_taxi_1.y - taxi1.getHeight()/2, p);
            mTaxys.add(geoT1);
            
            // Pintar Taxi2:
            GeoPoint geoT2 = new GeoPoint(41386497, 2166367);
            Point point_taxi_2 = new Point();
            projection.toPixels(geoT2, point_taxi_2);
            
            Bitmap taxi2 = BitmapFactory.decodeResource(
                    mapView.getResources(), 
                    R.drawable.logo_taxi);
            canvas.drawBitmap(taxi2, point_taxi_2.x - taxi2.getWidth()/2,point_taxi_2.y - taxi2.getHeight()/2, p);
            mTaxys.add(geoT2);
            
            // Pintar Taxi3:
            GeoPoint geoT3 = new GeoPoint(41385832, 2170068);
            Point point_taxi_3 = new Point();
            projection.toPixels(geoT3, point_taxi_3);
            
            Bitmap taxi3 = BitmapFactory.decodeResource(
                    mapView.getResources(), 
                    R.drawable.logo_taxi);
            canvas.drawBitmap(taxi3, point_taxi_3.x - taxi3.getWidth()/2,point_taxi_3.y - taxi3.getHeight()/2, p);
            mTaxys.add(geoT3);
        }
    }
    
    public void set_point(GeoPoint p){
        point_act = p;
    }
    
    public void set_TaxiStop(GeoPoint geoTS){
        geoStop = geoTS;
    }
    
    public void setTaxiPoint(GeoPoint geoT){
        geoT1 = geoT;
    }
    
    public GeoPoint getNearestTaxy() {
        GeoPoint to = null;
        double dist;
        if (mTaxys!=null) {
            to = mTaxys.get(0);
            dist = CalculationByDistance(point_act, to);
            for (int i = 1; i < mTaxys.size(); i++) {
                double dist2 = CalculationByDistance(point_act,mTaxys.get(i));
                if (dist2 < dist) {
                    dist = dist2;
                    to = mTaxys.get(i);
                } 
            }
        }
        return to;
    }
    
    @Override
    public boolean onTap(GeoPoint point, MapView mapView) 
    {
        Context contexto = mapView.getContext();
        String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " + 
                     "Lon: " + point.getLongitudeE6()/1E6;
        Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
        toast.show();
        
        return true;
    }
    /*private void drawTaxys(Canvas canvas, MapView mapView, boolean shadow) {
        
    }*/
    
    public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
        double lat1 = StartP.getLatitudeE6()/1E6;
        double lat2 = EndP.getLatitudeE6()/1E6;
        double lon1 = StartP.getLongitudeE6()/1E6;
        double lon2 = EndP.getLongitudeE6()/1E6;
        double dLat = Math.toRadians(lat2-lat1);
        double dLon = Math.toRadians(lon2-lon1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
            Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double earthRadius = 6371;
        return earthRadius * c;
     }
}
